{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 算术运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "0\n",
      "8\n",
      "0.5\n",
      "5.5\n",
      "5\n",
      "1\n",
      "4\n",
      "8\n",
      "2\n",
      "2\n",
      "-3\n",
      "-3\n",
      "-3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "print(1+1)  # 加法运算\n",
    "print(1-1)  # 减法运算\n",
    "print(2*4)  # 乘法运算\n",
    "print(1/2)  # 除法运算\n",
    "print(11/2)  # 5.5，除法运算\n",
    "print(11// 2)  # 5，整除运算\n",
    "print(11%2)  # 取余运算\n",
    "print(2**2)  # 表示的是2的2次方\n",
    "print(2**3)  # 表示的是2的3次方\n",
    "\n",
    "# 存在正负数的情况\n",
    "print(9//4)  # 2\n",
    "print(-9//-4)  # 2\n",
    "# 一正一负整除，向下取整\n",
    "print(9//-4)  # -3\n",
    "print(-9//4)  # -3\n",
    "# 一正一负取余，公式：余数=被除数-除数*商\n",
    "print(9%-4)  # -3\n",
    "print(-9%4)  # 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "20 1653236512\n",
      "20 1653236512\n",
      "20 1653236512\n",
      "50\n",
      "40\n",
      "80\n",
      "<class 'int'>\n",
      "26.666666666666668\n",
      "<class 'float'>\n",
      "13.0\n",
      "1.0\n",
      "20 30 40\n",
      "交换之前： 10 20\n",
      "交换之后： 20 10\n"
     ]
    }
   ],
   "source": [
    "# 赋值运算符，执行顺序：从右到左\n",
    "i = 3 + 4\n",
    "print(i)\n",
    "\n",
    "# 链式赋值\n",
    "a = b = c = 20\n",
    "print(a, id(a))  # id为内存地址，三个变量指向相同的地址\n",
    "print(b, id(b))\n",
    "print(c, id(c))\n",
    "\n",
    "# 参数赋值\n",
    "a = 20\n",
    "a += 30  # a=a+30\n",
    "print(a)\n",
    "a -= 10\n",
    "print(a)  # a=a-10\n",
    "a *= 2\n",
    "print(a)  # a=a*2\n",
    "print(type(a))  # int\n",
    "a /= 3\n",
    "print(a)\n",
    "print(type(a))  # float\n",
    "a //= 2\n",
    "print(a)  # float\n",
    "a %= 3\n",
    "print(a)\n",
    "\n",
    "# 系列解包赋值，要求等号左右数量相同，注意顺序不能错\n",
    "a, b, c = 20, 30, 40\n",
    "print(a, b, c)\n",
    "# 解包赋值应用在交换两个变量的值，不需要中间变量\n",
    "a, b = 10, 20\n",
    "print('交换之前：', a, b)\n",
    "a, b = b, a  # 交换\n",
    "print('交换之后：', a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>b吗？ False\n",
      "a<b吗？ True\n",
      "a>=b吗？ False\n",
      "a<=b吗？ True\n",
      "a==b吗？ False\n",
      "a!=b吗？ True\n",
      "True\n",
      "True\n",
      "True\n",
      "False\n",
      "2821453446984\n",
      "2821453178440\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 比较运算符，结果为bool类型\n",
    "a, b = 10, 20\n",
    "print('a>b吗？', a > b)  # False\n",
    "print('a<b吗？', a < b)  # True\n",
    "print('a>=b吗？', a >= b)  # False\n",
    "print('a<=b吗？', a <= b)  # True\n",
    "print('a==b吗？', a == b)  # False\n",
    "print('a!=b吗？', a != b)  # True\n",
    "\n",
    "# 一个=为赋值，两个==为比较\n",
    "# 一个变量由三个部分组成，标识，类型，值\n",
    "# ==比较的是值，比较对象的标识使用is\n",
    "a = 10\n",
    "b = 10\n",
    "print(a == b)  # True，说明a与b的value同\n",
    "print(a is b)  # True，说明a与b的id同\n",
    "# 内存赋值时去内存里找有没有这个对象，有的话就不重新创建，给变量相同id即可\n",
    "lst1 = [11, 22, 33, 44]\n",
    "lst2 = [11, 22, 33, 44]\n",
    "print(lst1 == lst2)  # True\n",
    "print(lst1 is lst2)  # False\n",
    "print(id(lst1))\n",
    "print(id(lst2))\n",
    "\n",
    "# is not 不同为Ture\n",
    "print(a is not b)  # False，a的id与b同\n",
    "print(lst1 is not b)  # True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 布尔运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "True\n",
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a, b = 1, 2\n",
    "\n",
    "# and 并且\n",
    "print(a == b and b == 2)  # True and True ---> True\n",
    "print(a == 1 and b < 2)  # False\n",
    "print(a != 1 and b == 2)  # False\n",
    "print(a !=1 and b != 2)  # False\n",
    "\n",
    "# or 或者\n",
    "print(a == 1 or b == 2)  # T\n",
    "print(a == 1 or b < 2)  # T\n",
    "print(a != 1 or b == 2)  # T\n",
    "print(a != 1 or b != 2)  # F\n",
    "\n",
    "# not 取反，针对bool类型操作数\n",
    "f = True\n",
    "ff = False\n",
    "print(not f)  # F\n",
    "print(not ff)  # T\n",
    "\n",
    "# in\n",
    "# not in\n",
    "s = 'helloworld'\n",
    "print('w' in s)\n",
    "print('k' in s)\n",
    "print('w' not in s)\n",
    "print('k' not in s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 位运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "12\n",
      "8\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 按位与&，同为1时为1\n",
    "print(4 & 8)\n",
    "\n",
    "# 按位或|，有1时为1\n",
    "print(4 | 8)\n",
    "\n",
    "# <<左移运算符，高位溢出舍弃，低位补0\n",
    "# 4左移1位\n",
    "print(4 << 1)\n",
    "\n",
    "# >>右移运算符，低位溢出舍弃，高位补0\n",
    "# 4右移1位\n",
    "print(4 >> 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 运算符优先级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 算术运算 > 位运算 > 比较运算 > 布尔运算\n",
    "# 有括号先括号"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
